package br.trainee.web.componentes;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import br.trainee.web.util.ResponseWrapper;

public class ItemAccordionTag extends BodyTagSupport{

	private String corpoItemAccordion;
	private String title;
	private String idHeader;
	private String idBody;
	private String closable;
	private String create;
	

	@Override
	public int doEndTag() throws JspException {

		// AQUI VOC~E PEGA O CORPO INTEIRO - SERÁ UM INCLUDE
		//
		String corpoItemAccordion = getBodyContent().getString();


		// PAGECONTEXT É PROTECTED
		JspWriter saida = pageContext.getOut();
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		HttpServletResponse response = (HttpServletResponse) pageContext.getResponse();
		
		request.setAttribute("bodyItemAccordion", corpoItemAccordion);
		request.setAttribute("title", title);
		request.setAttribute("idHeader", idHeader);
		request.setAttribute("idBody", idBody);
		request.setAttribute("closable", closable);
		request.setAttribute("create", create);

		ResponseWrapper responseWrapper = new ResponseWrapper(response);

		// APÓS A EXECUÇÃO ELE IRÁ INTERPRETAR A SAIDA
		responseWrapper.getString();

		RequestDispatcher req = request.getRequestDispatcher("/resources/componentes-jsp/accordion/itemAccordion.jsp");

		try {

			req.include(request, responseWrapper);

			String conteudoComponente = responseWrapper.getString();
			
			saida.println(conteudoComponente);

		} catch (IOException e1) {

			e1.printStackTrace();

		} catch (ServletException e1) {

			e1.printStackTrace();

		}

		return super.doEndTag();
	}

	// ************************************* GET AND SET *******************************/
	
	public String getCorpoItemAccordion() {
		return corpoItemAccordion;
	}

	public void setCorpoItemAccordion(String corpoItemAccordion) {
		this.corpoItemAccordion = corpoItemAccordion;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getIdHeader() {
		return idHeader;
	}

	public void setIdHeader(String idHeader) {
		this.idHeader = idHeader;
	}

	public String getIdBody() {
		return idBody;
	}

	public void setIdBody(String idBody) {
		this.idBody = idBody;
	}

	public String getClosable() {
		return closable;
	}

	public void setClosable(String closable) {
		this.closable = closable;
	}

	public String getCreate() {
		return create;
	}

	public void setCreate(String create) {
		this.create = create;
	}
	
	
}
